package org.blog.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.blog.dao.AuthorDAO;
import org.blog.db.DatabaseConnection;
import org.blog.idao.IAuthorDAO;
import org.blog.model.Author;

public class AuthorServlet extends HttpServlet {

    private IAuthorDAO authorDAO;
    private String authorList = "/author/list.jsp";
    private String authorNew = "/author/new.jsp";
    private String authorShow = "/author/show.jsp";
    private String authorUpdate = "/author/update.jsp";

    public AuthorServlet() {
        try {
            authorDAO = new AuthorDAO(DatabaseConnection.getInstance().getConnection());
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String action = request.getParameter("do");

            if (action != null) {
                if (action.equalsIgnoreCase("new")) {
                    RequestDispatcher rd = request.getRequestDispatcher(authorNew);
                    rd.forward(request, response);
                } else if (action.equalsIgnoreCase("show")) {
                    String id = request.getParameter("id");
                    if (id != null) {
                        Author a = authorDAO.getById(Integer.parseInt(id));
                        request.setAttribute("data", a);
                        RequestDispatcher rd = request.getRequestDispatcher(authorShow);
                        rd.forward(request, response);
                    }

                } else if (action.equalsIgnoreCase("delete")) {
                    String id = request.getParameter("id");
                    if (id != null) {
                        authorDAO.delete(Integer.parseInt(id));
                        List<Author> data = authorDAO.getAll();
                        request.setAttribute("data", data);
                        RequestDispatcher rd = request.getRequestDispatcher(authorList);
                        rd.forward(request, response);
                    }

                } else if (action.equalsIgnoreCase("update")) {
                    String id = request.getParameter("id");
                    if (id != null) {
                        Author a = authorDAO.getById(Integer.parseInt(id));
                        request.setAttribute("data", a);
                        RequestDispatcher rd = request.getRequestDispatcher(authorUpdate);
                        rd.forward(request, response);
                    }

                } else {
                    List<Author> data = authorDAO.getAll();
                    request.setAttribute("data", data);
                    RequestDispatcher rd = request.getRequestDispatcher(authorList);
                    rd.forward(request, response);
                }
            } else {
                List<Author> data = authorDAO.getAll();
                request.setAttribute("data", data);
                RequestDispatcher rd = request.getRequestDispatcher(authorList);
                rd.forward(request, response);
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String realName = request.getParameter("realName");
            String about = request.getParameter("about");

            Author a = new Author();
            a.setUsername(username);
            a.setPassword(password);
            a.setRealName(realName);
            a.setAbout(about);

            String id = request.getParameter("id");
            if (id != null) {
                authorDAO.update(Integer.parseInt(id), a);
            } else {
                authorDAO.insert(a);
            }


            List<Author> data = authorDAO.getAll();
            request.setAttribute("data", data);
            RequestDispatcher rd = request.getRequestDispatcher(authorList);
            rd.forward(request, response);

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}
